﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Protal.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.Protal.DataAccess;
using Microhard.Protal.DTO;
using Microhard.Core;
using Microhard.Protal.Resource;

namespace Microhard.Protal.Impl
{
    public class ArticleImpl : IArticle
    {
        public Core.DBUtility.ExecutedResult Add(Models.ArticleModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;

            try
            {
                db.Microhard_Article.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add("IArticle.Add", Msg.Impl_Add);
                Log.Error("IArticle.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.ArticleModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var entity = db.Microhard_Article.SingleOrDefault(c => c.ArticleID == model.ArticleID);

            if (entity != null)
            {
                entity.Title = model.Title;
                entity.Description = model.Description;
                entity.ArticleContent = model.ArticleContent;
                entity.CreateTime = DateTime.Now;
                entity.ArticleType = model.ArticleType;
                entity.IsOnIndex = model.IsOnIndex;
                entity.VideoUrl = model.VideoUrl;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                rtn.ErrorMsg.Add("IArticle.Update", Msg.Impl_Update);
                Log.Error("IArticle.Update", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var toDeltes = from b in db.Microhard_Article
                           where ids.Contains(b.ArticleID)
                           select b;
            foreach (var d in toDeltes)
            {
                db.Microhard_Article.DeleteObject(d);
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add("IArticle.Delete", Msg.Impl_Delete);
                Log.Error("IArticle.Delete", ex);
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(Models.ArticleModel model)
        {
            throw new NotImplementedException();
        }

        public Models.ArticleModel GetModelByID(Guid id)
        {
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var rtn = db.Microhard_Article.SingleOrDefault(p => p.ArticleID == id);
            if (rtn != null)
            {
                return rtn.ToModel();
            }

            return null;
        }

        public Models.ArticleModel GetModelByModel(Models.ArticleModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.ArticleModel> Query(Models.ArticleModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var entities = from b in db.Microhard_Article
                           select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pagedEntities.Select(m => m.ToModel()).ToList();
        }

        public List<Models.ArticleModel> Query(Models.ArticleModel Model)
        {
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var entities = from b in db.Microhard_Article
                           select b;
            if (Model != null && Model.ArticleType != Guid.Empty)
            {
                entities = from e in entities
                           where e.ArticleType == Model.ArticleType
                           select e;
            }
            return entities.ToList().Select(m => m.ToModel()).ToList();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

        public List<Models.ArticleModel> GetTopArticles(Models.ArticleModel model, int top)
        {
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var entities = from b in db.Microhard_Article
                           select b;
            if (model != null && model.IsOnIndex)
            {
                entities = from e in entities
                           where e.IsOnIndex == true
                           select e;
            }
            return entities.OrderByDescending(c => c.CreateTime).Take(top).ToList().Select(m => m.ToModel()).ToList();
        }
    }
}
